<template>
	<view class="page_content">
		<view class="bottom-view_2">
			<view class="bottom_btn_2" @click.stop="addNewReceiver">添加接收人</view>
			<view class="bottom_btn" @click.stop="saveSetting">保存设置</view>
		</view>
		
		<template v-if="rows.length > 0">
			<view class="receiver_content">
				<scroll-view scroll-y :show-scrollbar="false" enhanced="true" style="height: 100%;">
					<view class="receiver_item" :style="index == 0 ? 'margin-top:32rpx' : ''"  v-for="(item,index) in rows" :key="index" :data-item="item">
						<view class="receiver_item_content" @click.stop="selectUser(index)">
							<view class="receiver_item_left">
								<image src="/static/home/user.png" mode="aspectFit" style="width: 26rpx;height: 28rpx;"></image>
								<input class="input_css" type="text" disabled v-model="item.desc" placeholder="请选择通知接收人" placeholder-style="color:#9CA3AF" />
							</view>
							<view class="receiver_item_right" @click.stop="deleteUser(index)">
								<image src="/static/mine/ntf_delete.png" mode="aspectFit" style="width: 18rpx;height: 28rpx;"></image>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</template>
		<template v-else>
			<view class="nodata">
				<view class="img"></view>
				<view class="text">暂无数据</view>
			</view>
		</template>

		
		<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0">
			<view class="popup_custom_content">
				<view class="popup_custom_title">请选择接收人</view>
				<view class="scroll_content">
					<scroll-view scroll-y :show-scrollbar="false" enhanced="true" style="height: 100%;">
						<view class="user_item" :class="(index == users.length - 1)? 'user_item_line' : ''" v-for="(item,index) in users" :key="index" :data-item="item" @click.stop="itemClick">
							{{item.desc}}
						</view>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rows:[],
				users:[],
				currentIndex:0
			}
		},
		onLoad() {
			let self = this;
			self.$progressHud.showLoading();
			self.$NetApi.getMethod('/loyu-api/app/item/is-bind',{
				itemId:this.$Route.query.id | ''
			}).then(ok => {
				self.$progressHud.hideLoading();
				self.rows = ok.data;
			}, fail => {
				self.$progressHud.hideLoading();
				self.$progressHud.showToast(fail.errMsg || '网络错误');
			})
		},
		methods: {
			saveSetting(e){
				var items = this.rows.filter(it=>{
					return (it && it.code) ? true : false
				});
				items = items.map(it=>{
					return it.code
				});
				
				let self = this;
				self.$progressHud.showLoading();
				self.$NetApi.postMethod('/loyu-api/app/item/add-item-notice',{
					itemId:this.$Route.query.id | '',
					userIds:items
				}).then(ok => {
					self.$progressHud.hideLoading();
					self.$progressHud.showToast('保存成功')
				}, fail => {
					self.$progressHud.hideLoading();
					self.$progressHud.showToast(fail.errMsg || '网络错误');
				})
			},
			addNewReceiver(e){
				this.rows.push({})
			},
			deleteUser(index){
				this.rows.splice(index, 1);
			},
			selectUser(index){
				this.currentIndex = index;
				
				let self = this;
				self.$progressHud.showLoading();
				self.$NetApi.getMethod('/loyu-api/app/item/no-bind',{
					itemId:this.$Route.query.id | ''
				}).then(ok => {
					self.$progressHud.hideLoading();
					self.users = ok.data;
				}, fail => {
					self.$progressHud.hideLoading();
					self.$progressHud.showToast(fail.errMsg || '网络错误');
				})
				this.$refs.popup.open()
			},
			itemClick(e){
				this.$refs.popup.close()
				
				let rawItem = this.rows[this.currentIndex]
				rawItem.desc=e.currentTarget.dataset.item.desc
				rawItem.code=e.currentTarget.dataset.item.code
				this.$set(this.rows, this.currentIndex, rawItem);
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #FFF;
	}
	.page_content{
		width: 100%;
		height: 100%;
		overflow: hidden;
		position: relative;
	}
	
	.bottom-view_2{
		position: fixed;
		left: 0px;
		right: 0px;
		bottom: 0px;
		border-top: 0.5px solid #e6e6e6;
		z-index: 2;
		
		background-color: #FFF;
		padding-left: 32rpx;
		padding-right: 32rpx;
		padding-top: 32rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
		
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		column-gap: 32rpx;
	}
	.bottom_btn{
		border-radius: 8rpx;
		background: $power-color-primary;
		width: 100%;
		text-align: center;
		color: #FFF;
		
		
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		height: 90rpx;
		line-height: 90rpx;
		letter-spacing: normal;
	}
	.bottom_btn_2{
		border-radius: 8rpx;
		background: #FFF;
		width: 100%;
		text-align: center;
		color: $power-color-primary;
		border: 2rpx solid $power-color-primary;
		
		font-family: Roboto;
		font-size: 28rpx;
		font-weight: normal;
		height: 90rpx;
		line-height: 90rpx;
		letter-spacing: normal;
	}
	.receiver_content{
		height: calc(100% - env(safe-area-inset-bottom) - 32rpx - 90rpx - 32rpx);
		width: 100%;
		overflow: hidden;
	}
	.receiver_item{
		padding: 0px 32rpx 32rpx 32rpx;
		width: 100%;
	}
	.receiver_item_content{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		background-color: #F9FAFB;;
		padding: 16rpx 24rpx;
		border-radius: 8rpx;
	}
	.receiver_item_left{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		column-gap: 16rpx;
	}
	.receiver_item_right{
		height: 40rpx;
		width: 50rpx;
		display: flex;
		flex-direction: row-reverse;
		justify-content: flex-start;
		align-content: center;
		flex-wrap: wrap;
	}
	.input_css{
		font-size: 28rpx;
		font-weight: normal;
		line-height: 40rpx;
		letter-spacing: normal;
		color: #000000;
	}
	.popup_custom_content{
		width: 100%;
		height: 70vh;
		overflow: hidden;
	}
	.popup_custom_title{
		width: 100%;
		height: 90rpx;
		border-bottom: 0.5px solid #e5e5e5;
		text-align: center;
		line-height: 80rpx;
		
		font-size: 32rpx;
		font-weight: normal;
	}
	.scroll_content{
		width: 100%;
		height: calc(100% - 90rpx);
		overflow: hidden;
	}
	.user_item{
		padding: 24rpx 32rpx;
		border-bottom: 0.5px solid #e5e5e5;
		
		font-size: 28rpx;
		font-weight: normal;
		line-height: 40rpx;
		letter-spacing: normal;
	}
	.user_item_line{
		border-bottom: 0.5px solid #e5e5e5;
	}
	.nodata {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: transparent;
		padding-bottom: 25%;
	}
	
	.nodata .img {
		width: 50vw;
		height: 41vw;
		margin-bottom: 10rpx;
		background: url(/static/mine/no_data.png) no-repeat center center;
		background-size: 100% auto;
	}
	
	.nodata .text {
		font-size: 30rpx;
		color: #999;
	}
</style>
